import 'package:flutter_app_demo/extension/common.dart';
import 'package:flutter_app_demo/helper/route_helper.dart';
import 'package:flutter_app_demo/pages/home/home_page.dart';
import 'package:flutter_app_demo/pages/image/image_picker_page.dart';
import 'package:flutter_app_demo/pages/image/image_view_page.dart';
import 'package:flutter_app_demo/pages/live/live_camera_push_page.dart';
import 'package:flutter_app_demo/pages/live/live_page.dart';
import 'package:flutter_app_demo/pages/login/login_page.dart';
import 'package:flutter_app_demo/pages/one/one_page.dart';
import 'package:flutter_app_demo/pages/splash/splash_page.dart';
import 'package:flutter_app_demo/pages/web/common_web_page.dart';
import 'package:go_router/go_router.dart';

// Created by 王亮 on 2025/2/8.
final GoRouter router = GoRouter(
  observers: [
    RouteHelper.instance.routeObserver,
  ],
  routes: <RouteBase>[
    GoRoute(
      path: '/',
      name: '启动页',
      builder: (context, state) {
        return SplashPage();
      },
    ),
    GoRoute(
      name: '登录页',
      path: '/login',
      builder: (context, state) {
        return LoginPage();
      },
    ),
    GoRoute(
      name: '直播首页',
      path: '/live',
      builder: (context, state) {
        return LivePage();
      },
    ),
    GoRoute(
      name: '直播摄像头推流',
      path: '/live/push/camera',
      builder: (context, state) {
        return LiveCameraPushPage();
      },
    ),
    GoRoute(
      name: '首页',
      path: '/home',
      builder: (context, state) {
        return HomePage();
      },
    ),
    GoRoute(
        name: 'one',
        path: '/one',
        builder: (context, state) {
          final Map<String, String> params = state.uri.queryParameters;
          return OnePage(
            data: params,
          );
        }),
    GoRoute(
        path: '/web',
        name: 'web',
        builder: (context, state) {
          final Map<String, String> params = state.uri.queryParameters;
          final String url = params['url'] ?? "";
          return CommonWebPage(
            url: url,
          );
        }),
    GoRoute(
        path: '/image/picker',
        name: 'image/picker',
        builder: (context, state) {
          return ImagePickerPage();
        }),
    GoRoute(
        path: '/image/view',
        name: 'image/view',
        builder: (context, state) {
          final Map<String, String> params = state.uri.queryParameters;
          final index = params['index']?.toInt() ?? 0;
          final List<String> urls = (params['urls'] ?? "").split("|");
          List<String> uris = [];
          for (int i = 0; i < urls.length; i++) {
            uris.add(Uri.decodeComponent(urls[i]));
          }
          return ImageViewPage(uris: uris, initialPage: index);
        }),
  ],
);
